package cn.his.medical.pacshism.adapter;


import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.his.medical.pacshism.R;
import cn.his.medical.pacshism.bean.ViewPagerBean;
import cn.his.medical.pacshism.view.AutoViewPager;

/**
 * 作者：xiewei on 2018/3/5 16:52
 * 邮箱：xiewei19910115@163.com
 */
public abstract class BaseViewPagerAdapter <T> extends PagerAdapter implements ViewPager.OnPageChangeListener{

    private List<T> data = new ArrayList<>();

    private Context mContext;
    private AutoViewPager mView;

    private OnAutoViewPagerItemClickListener listener;

    public BaseViewPagerAdapter(Context context, List<T> data) {
        this.mContext = context;
        this.data = data;
    }

    public BaseViewPagerAdapter(Context context, List<T> data,OnAutoViewPagerItemClickListener listener) {
        this.mContext = context;
        this.data = data;
        this.listener = listener;
    }

    public void init(AutoViewPager viewPager, BaseViewPagerAdapter adapter){
        mView = viewPager;
        mView.setAdapter(this);
        mView.addOnPageChangeListener(this);

        if (data == null || data.size() == 0){
            return;
        }
        //设置初始为中间，这样一开始就能够往左滑动了
        int position = Integer.MAX_VALUE/2 - (Integer.MAX_VALUE/2) % getRealCount();
        mView.setCurrentItem(position);

        mView.start();
        mView.updatePointView(getRealCount());
    }

    public void setListener(OnAutoViewPagerItemClickListener listener) {
        this.listener = listener;
    }

    public void add(T t){
        data.add(t);
        notifyDataSetChanged();
//        mView.updatePointView(getRealCount());
    }

    @Override
    public int getCount() {
        return data == null ? 0 : Integer.MAX_VALUE;
    }

    public int getRealCount(){
        return data == null ? 0 : data.size();
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        View view = (View) object;
        container.removeView(view);

    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        View view =  LayoutInflater.from(mContext)
                .inflate(R.layout.view_pager_item,container,false);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onItemClick(position % getRealCount(),data.get(position % getRealCount()));
                }
            }
        });
        ViewPagerBean viewPagerBean = (ViewPagerBean) data.get(position);
        ImageView img_user = view.findViewById(R.id.img_user);
        TextView t_sub_head = view.findViewById(R.id.t_sub_head);
        TextView t_user_name = view.findViewById(R.id.t_user_name);
        TextView t_intro = view.findViewById(R.id.t_intro);
        TextView t_show_time = view.findViewById(R.id.t_show_time);
        TextView t_show_address = view.findViewById(R.id.t_show_address);
        TextView t_show_phone = view.findViewById(R.id.t_show_phone);
        loadImage(img_user,position, viewPagerBean.getUrl());
        t_sub_head.setText(viewPagerBean.getT_sub_head());
        t_user_name.setText(viewPagerBean.getT_user_name());
        t_intro.setText(viewPagerBean.getT_intro());
        t_show_time.setText(viewPagerBean.getT_show_time());
        t_show_address.setText(viewPagerBean.getT_show_address());
        t_show_phone.setText(viewPagerBean.getT_show_phone());
        container.addView(view);
        return view;
    }

    public abstract void loadImage(ImageView view, int position, String url);

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
//        mView.onPageSelected(position % getRealCount());
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    public interface OnAutoViewPagerItemClickListener<T> {
        void onItemClick(int position,T t);
    }
}

